package com.bob.clan.wxconnector.component;

import com.bob.clan.wxconnector.entity.Mp;
import com.bob.clan.wxconnector.mapper.MpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class MpComponent {
    @Autowired
    private MpMapper mapper;

    private List<Mp> mpList;

    //mp不常用的数据，可以静态化存储，不必每次都都数据库
    public Mp getBySysCode(String sysCode){
        if(mpList==null || mpList.size()==0){
            mpList = mapper.selectList(null);
        }
        for (Mp mp : mpList) {
            if (sysCode.equals(mp.getSysCode())){
                return mp;
            }
        }
        throw new RuntimeException(sysCode+":该sysCode未配置Mp信息，请查看数据后台");
    }

    public Mp getByAppid(String appid){
        if(mpList==null || mpList.size()==0){
            mpList = mapper.selectList(null);
        }
        for (Mp mp : mpList) {
            if (appid.equals(mp.getAppid())){
                return mp;
            }
        }
        throw new RuntimeException(appid+":该appid未配置Mp信息，请查看数据后台");
    }

    public List<Mp> getListSingleOriginalId() {
        if(mpList==null || mpList.size()==0){
            mpList = mapper.selectList(null);
        }
        Set<String> seenOriginalIds = new HashSet<>();
        List<Mp> resultList = new ArrayList<>();
        for (Mp mp : mpList) {
            String originalId = mp.getOriginalId();
            if (!seenOriginalIds.contains(originalId)) {
                seenOriginalIds.add(originalId);
                resultList.add(mp);
            }
        }
        return resultList;
    }

    public List<Mp> getListByOriginalId(String originalId) {
        if(mpList==null || mpList.size()==0){
            mpList = mapper.selectList(null);
        }
        List<Mp> resultList = new ArrayList<>();
        for (Mp mp : mpList) {
            if (mp.getOriginalId().equals(originalId)) {
                resultList.add(mp);
            }
        }
        return resultList;
    }
}
